package com.example.amoxicilin.null_test.main;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.amoxicilin.null_test.R;
import com.example.amoxicilin.null_test.domain.Resource;
import com.example.amoxicilin.null_test.tools.HttpUtils;
import com.example.amoxicilin.null_test.tools.JsonTools;

import java.util.List;

public class CommunicationFragment extends Fragment {

    private RecyclerView recyclerView;
    private List<String> comlist;


    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg){
            super.handleMessage(msg);
            String response=(String) msg.obj;
            comlist=JsonTools.jsonSplit(response);
            Log.i("234",comlist.toString());
            recyclerView.setAdapter(new RecyclerView.Adapter() {

                class MyViewHolder extends RecyclerView.ViewHolder{

                    public final ImageView clas;
                    public final TextView title;
                    public final TextView info;
                    public MyViewHolder(View itemView) {
                        super(itemView);
                        clas=(ImageView)itemView.findViewById(R.id.resource_label);
                        title=(TextView)itemView.findViewById(R.id.com_title);
                        info=(TextView)itemView.findViewById(R.id.com_info);
                    }
                }


                @Override
                public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext())
                            .inflate(R.layout.item_com, parent, false);
                    return new MyViewHolder(view);
                }

                @Override
                public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {
                    MyViewHolder myViewHolder=(MyViewHolder) holder;
                    Log.i("resource", "hell"+comlist.get(position));
                    final Resource study = JsonTools.getResource(null, comlist.get(position));
                    myViewHolder.title.setText(study.getTitle());
                    myViewHolder.info.setText(study.getIntroduction());
                    myViewHolder.info.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
                    Log.i("test", "label"+position+" "+study.getLabel1());
                    switch (study.getLabel1()){
                        case 1:
                            myViewHolder.clas.setImageResource(R.drawable.hua);break;
                        case 2:
                            myViewHolder.clas.setImageResource(R.drawable.ji);break;

                        default:break;
                    }

                    holder.itemView.setOnClickListener(new View.OnClickListener(){

                        @Override
                        public void onClick(View v) {
                            Intent intent =new Intent();
                            intent.setClass(getActivity(),StudyActivity.class);
                            Bundle bundle=new Bundle();
                            bundle.putString("Position",study.getIntroduction());
                            bundle.putInt("ResourceId",study.getNumber());
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }
                    });
                }

                @Override
                public int getItemCount() {
                    return comlist.size();
                }
            });
        }
    };




    @Override
    public View onCreateView(LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        initData();
        View rootview=inflater.inflate(R.layout.fragment_comunication,container, false);
        Context context = rootview.getContext();
        recyclerView = (RecyclerView) rootview.findViewById(R.id.com_rec);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        SearchView searchView=(SearchView)rootview.findViewById(R.id.com_search);
        searchView.setSubmitButtonEnabled(true);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                Intent intent=new Intent();
                intent.setClass(getActivity(),ComSearchActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("Search",query);
                intent.putExtras(bundle);
                startActivity(intent);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        return rootview;
    }

    private void initData(){
        String path = "http://192.168.12.221:8080/tmoocserver/ContentServlet?classid="+-1;
        String jsonString = HttpUtils.getJsonContent(path);

        Log.i("123",jsonString);
        Message message= new Message();
        message.obj=jsonString;
        handler.sendMessage(message);

    }

}

